package DoublePointer;

import org.junit.Test;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: DoublePointer
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-22  08:53
 * @Description: TODO
 * @Version: 1.0
 */
public class 盛最多水的容器11 {

    // 这个属于暴力解法
    public int maxArea(int[] height) {
        if (height.length <= 1) {
            return 0;
        }
        int maxArea = 0;
        for (int i = 0; i < height.length; i++) {
            for (int j = height.length - 1; j >= 0; j--) {
                maxArea = Math.max(maxArea, (j - i) * Math.min(height[i], height[j]));
            }
        }
        return maxArea;
    }

    // 通过双指针来实现那获取最大的面积
    public int maxArea2(int[] height) {
        int max = 0;
        int left = 0, right = height.length-1;
        while (left <= right) {
            max = Math.max(max, Math.min(height[left], height[right]) * (right - left));
            if (height[left] < height[right]) {
                left++;
            } else {
                right--;
            }
        }
        return max;
    }

    @Test
    public void test() {
        int i = maxArea2(new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7});
        System.out.println(i);
    }
}
